package ufc.quixada.petti.service;

import java.util.List;

import ufc.quixada.petti.dao.EventoDAO;
import ufc.quixada.petti.infra.SimplesEntityManager;
import ufc.quixada.petti.model.Evento;


public class EventoService {

	private EventoDAO dao;

	private SimplesEntityManager simpleEntityManager;

	public EventoService() {

	}

	public EventoService(SimplesEntityManager simpleEntityManager) {
		this.simpleEntityManager = simpleEntityManager;
		dao = new EventoDAO(simpleEntityManager.getEntityManager());
	}

	public void save(Evento evento) {

		try {
			simpleEntityManager.beginTransaction();
			dao.save(evento);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		}

	}

	public List<Evento> findAll() {
		return dao.findAll();
	}

	public void atualizar(Evento evento) {

		try {
			simpleEntityManager.beginTransaction();
			dao.update(evento);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		} finally {
			simpleEntityManager.close();
		}
	}

	public void remover(Evento evento) {
		try {
			simpleEntityManager.beginTransaction();
			dao.delete(evento);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		} finally {
			simpleEntityManager.close();
		}
	}

        
        
        
        
	public Evento encontrarPorId(Long id) {
		Evento evento = null;
		try {
			simpleEntityManager.beginTransaction();
			evento = dao.getById(id);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		} finally {
			simpleEntityManager.close();
		}
		return evento;
	}

	public Evento removerPorId(Long id) {
		Evento evento = null;
		try {
			simpleEntityManager.beginTransaction();
			evento = dao.getById(id);
			dao.delete(evento);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		} finally {
			simpleEntityManager.close();
		}
		return evento;
	}

	public Evento encontrarPorNome(String nome) {
		Evento evento = null;
		try {
			simpleEntityManager.beginTransaction();
			evento = dao.getByName(nome);
			simpleEntityManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			simpleEntityManager.rollBack();
		} finally {
			simpleEntityManager.close();
		}
		return evento;
	}

}
